package org.carrot2.examples.clustering;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.tartarus.snowball.ext.PorterStemmer;

import weka.core.Stopwords;

public class coOccurence {
	
	
	public HashMap<String,Integer> stemmedWordsCount;
	public HashMap<String,String> stemmedWords;
	public HashMap<String,HashMap<String,Integer>> adjacentWords;
	public static ArrayList<String> stopwords;
	public class entity{
		public String str;
		public int val;
	}
	
	public coOccurence()
	{
		stemmedWordsCount=new HashMap<String,Integer>();
		stemmedWords=new HashMap<String, String>();
		adjacentWords=new HashMap<String,HashMap<String,Integer>>();
		stopwords=new ArrayList<String>();
		stopwords.add("-");
		stopwords.add(":");
		stopwords.add(",");
		stopwords.add("'");
	}
	
	
	public String removePunctuation(String title){
		
		for (String abc: stopwords){
			title=title.replace(abc, " ");			
		}
		
		return title;
	}
	
	public void coOccuringwordsString(String param1){
		
		
		param1=removePunctuation(param1.toLowerCase());		
		
		PorterStemmer obj= new PorterStemmer();		
		
		StringTokenizer t1 = new StringTokenizer(param1);
		
		String prev=null;		
		
		while(t1.hasMoreTokens()){
			
			String t2=t1.nextToken();
			
			
			if(!Stopwords.isStopword(t2)){
				
				obj.setCurrent(t2);				
				obj.stem();
				String temp = obj.getCurrent();
				
				if(!stemmedWords.keySet().contains(temp)){
					
					stemmedWordsCount.put(temp, 1);
					stemmedWords.put(temp, t2);
										
					adjacentWords.put(temp, new HashMap<String,Integer>());										
				}
				
				else{
					int val=stemmedWordsCount.get(temp);
					stemmedWordsCount.put(temp, val+1);
					
				}
					
				
				if(prev!=null){
					
					
					if(!adjacentWords.get(temp).keySet().contains(prev)){
						
						adjacentWords.get(temp).put(prev, 1);
												
					}
					else
					{
						
						int val=adjacentWords.get(temp).get(prev);
						
						adjacentWords.get(temp).put(prev, val+1);

						
					}
					
					if(!adjacentWords.get(prev).keySet().contains(temp)){
						
						adjacentWords.get(prev).put(temp, 1);
						
						
					}
					else
					{
						int val=adjacentWords.get(prev).get(temp);
						adjacentWords.get(prev).put(temp, val+1);
						
					}
					
					
				}
				prev=temp;
											
			}
			
		}				
	}
	
	
	public ArrayList<entity> coOccuringWordsArrayList(ArrayList<String> param1){
		
		for(String abc:param1){
			coOccuringwordsString(abc);
		}
		
		ArrayList<entity> list=new ArrayList<entity>();
		
		for(String key:stemmedWordsCount.keySet()){
			
			entity obj1=new entity();
			obj1.str=key;
			obj1.val=stemmedWordsCount.get(key);
			list.add(obj1);
		}
		
	    Comparator<entity> intComparator = new Comparator<entity>() {
	        @Override public int compare(entity s1, entity s2) {
	        	if(s1.val>s2.val)
	        		return 0;
	        	else
	        		return 1;
	            
	        }           
	    };
		Collections.sort(list,intComparator);
		
		/*for(entity obj1:list){
			System.out.print(stemmedWords.get(obj1.str)+" = " +obj1.val+", ");
			ArrayList<entity> ll = new ArrayList<entity>();
			for(String key:adjacentWords.get(obj1.str).keySet()){
				
				entity e=new entity();
				e.str=key;
				e.val=adjacentWords.get(obj1.str).get(key);
				ll.add(e);
			}
			Collections.sort(ll,intComparator);
			System.out.print("::  [");
			for(entity obj2:ll){
				System.out.print(stemmedWords.get(obj2.str)+" = " +obj2.val+", ");
			}
			System.out.println("]");
			
		
		
	}*/
		entity obj1=list.get(0);
		
		
		ArrayList<entity> ll = new ArrayList<entity>();
		
		
		for(String key:adjacentWords.get(obj1.str).keySet()){
			
			entity e=new entity();
			e.str=key;
			e.val=adjacentWords.get(obj1.str).get(key);
			ll.add(e);
		}
		Collections.sort(ll,intComparator);
		
		return ll;
	
		
	
	
	}

}